//
//  SinaVisitorView.swift
//  weibo
//
//  Created by liuchao on 16/1/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SinaVisitorView: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
       setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
         backgroundColor = UIColor(white: 234/255.0, alpha: 1.0)
        
        addSubview(animationImageView)
        addSubview(houseImgeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        animationImageView.translatesAutoresizingMaskIntoConstraints = false
        houseImgeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //首页的小房子
        addConstraint(NSLayoutConstraint(item: houseImgeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImgeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //提示label --
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: animationImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        // 注册按钮 -- 宽 100,高 30 , 左边和 messageLabel的左边对其,顶部和其底部对齐
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
    }
    
    //懒加载
    
    private lazy var animationImageView:UIImageView = {
      
         let img = UIImageView()
        
         img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
    
         img.sizeToFit()
        
         return img
    }()
    
    // 设置动画
    
    func startAnimation(){
    
       let animation=CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount=MAXFLOAT
        animation.duration=10
        animation.toValue=2*M_PI
        
        animation.removedOnCompletion = false
        
        animationImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    ///  首页小房子
    private lazy var houseImgeView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
    }()
    
    ///  提示语
    private lazy var messageLabel: UILabel  = {
        
        let lb = UILabel()
        
        // 设置约束
        lb.text = "关注一些人，回这里看看有什么惊喜  关注一些人，回这里看看有什么惊喜"
        
        lb.textColor = UIColor.darkGrayColor()
        //NSTextAlignment.Center
        lb.textAlignment = .Center
        //lb.textAlignment = NSTextAlignment.Center
        //设置numberOfLines
        lb.numberOfLines = 0
        
        return lb
    }()
    
    
    ///  注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 添加方法
        button.addTarget(self , action: "registerMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
        
    }()
    
    ///  登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 按钮方法
        button.addTarget(self , action: "loginMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //MARK: -- 登录 or 注册 方法 --
    
    func loginMethod(){
    
         printLog("登录")
      
    }
    
    func registerMethod(){
        
        printLog("注册")
    }
    
    //MARK: -- 定义一个供子类调用的方法，便于重新设置文字与图片
    func setupInfo(message:String,image:String?){
        
        if let Oimg = image {
            
            animationImageView.hidden=true
            
            houseImgeView.image=UIImage(named: Oimg)
        
        }else{
            animationImageView.hidden=false
        }
        
        messageLabel.text=message
        
    }
    

}
